<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
</head>

<body>

    <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="guestName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">客户手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="guestPhone" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">房间号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="roomId" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-normal" lay-submit
                            lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </div>
            </form>
        </div>
    </fieldset>

    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
    </div>
    </script>

    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="picTpl">
    <img src="{{d.pic}}" name="pic" alt="">
    </script>

    <script src="/layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>
        layui.use(['form', 'table', 'tree', 'util'], function () {
            var table = layui.table,
                form = layui.form,
                $ = layui.jquery,
                tree = layui.tree,
                layer = layui.layer,
                util = layui.util;

            table.render({
                elem: '#test'
                , url: 'http://localhost:8080/order/table'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , initSort: {
                    field: 'id',
                    type: 'asc'
                }
                , title: '用户数据表'
                , cols: [[
                    { type: 'checkbox', id: "test12", class: "demo-tree-more" }
                    , { field: 'id', title: '订单号', unresize: true, sort: true }
                    , { field: 'guestName', title: '客户名' }
                    , { field: 'guestPhone', title: '客户手机号' }
                    , { field: 'roomId', title: '房间号', unresize: true, sort: true }
                    , { field: 'orderMoney', title: '消费金额', sort: true }
                    , { field: 'orderTime', title: '下单时间', sort: true }
                    , { title: '操作', toolbar: '#barDemo' }
                ]]
                , page: true
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('test', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        //searchParams: result
                        "guestName": data.field.guestName,
                        "guestPhone": data.field.guestPhone,
                        "roomId": data.field.roomId,
                    }
                }, 'data');

                return false;
            });

            //头工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;
                    case 'add':
                        layer.open({
                            title: '添加用户',
                            type: 2,
                            shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                            area: ['80%', '100%'],
                            fixed: true, //不固定
                            maxmin: true,
                            content: './orderAdd.html',
                            end: function (index) {
                                table.reload('test');
                            }
                        });
                        break;
                    case 'delete':
                        layer.confirm('真的删除选中的行么', function (index) {
                            var data = checkStatus.data,
                                id = [];
                            for (let i = 0; i < data.length; i++) {
                                id.push(data[i].id);
                            }

                            $.ajax({
                                url: "http://localhost:8080/order/delete",
                                data: { "id": JSON.stringify(id) },
                                success: function (result) {
                                    if (result.code == 200) {
                                        table.reload('test');
                                    } else {
                                        layer.msg(data.msg);
                                    }
                                },
                            });
                            
                            layer.close(index);
                        });
                        break;
                    // 导入excel表格
                    case 'LAYTABLE_TIPS':
                        layer.open({
                            title: '导入excel数据',
                            type: 2,
                            shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                            area: ['50%', '70%'],
                            fixed: true, //不固定
                            maxmin: true,
                            content: './excelUpload.html',
                            end: function (index) {
                                table.reload('test');
                            }
                        });
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data,
                    dataTree = "";

                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "http://localhost:8080/order/delete",
                            data: { "id": data.id },
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(data.msg);
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            dataType: JSON
                        })
                        obj.del();
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {

                    $.ajax({
                        url: "http://localhost:8080/order/getEdit",
                        data: { "id": data.id },
                        success: function (result) {
                            // 把数据存到 localStorage
                            localStorage.setItem("roomId", data.roomId)
                            localStorage.setItem("orderId", data.id)
                            localStorage.setItem("order", JSON.stringify(result));
                        }
                    });

                    layer.open({
                        title: "编辑订单",
                        shade: 0.2,
                        type: 2,
                        // 点别的地方 是否关闭当前 layer 
                        shadeClose: true,
                        area: ['50%', '100%'],
                        fixed: true, // 不固定
                        maxmin: true,
                        content: "orderEdit.html",
                        end: function (index) {
                            table.reload('test');
                        }
                    })
                } else if (obj.event === 'detail') {
                    $.ajax({
                        url: "http://localhost:8080/order/detail",
                        data: { "id": data.id },
                        success: function (result) {
                            localStorage.setItem("treeData", JSON.stringify(result));
                        }
                    });

                    layer.open({
                        title: " "
                        , type: 2
                        , content: 'orderDetail.html'
                        , area: ['360px', '100%']
                        , offset: 'rt' //右上角
                        , anim: 5
                        , shadeClose: true
                    });
                }
            });
        });
    </script>

</body>

</html>